package com.ocom.xfrecode.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ocom.common.constant.UnifiendPayUrl;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.xfrecde.GetUserOrdered;
import com.ocom.common.request.xfrecde.GetUserOrderedB;
import com.ocom.common.request.xfrecde.XfOrderVerificationRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.xfrecode.service.XfRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class XfRecordController {

    @Autowired
    private XfRecordService xfRecordService;

    @PostMapping(UnifiendPayUrl.URL_sveXfRecord)
    public Result sveXfRecord(@RequestBody EntityXfRecord entityXfRecord){
        try {
            Boolean saveXfRecord=xfRecordService.sveXfRecord(entityXfRecord);
            if (saveXfRecord){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        }catch (Exception e){
        //     log.info("请求方式:【{}】,模块入库新增的数据:【{}】,异常信息:【{}】","消费订单的成功数据入库的模块入库失败",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_802);
        }

    }

    @PostMapping(UnifiendPayUrl.URL_deleteXfRecord)
    public Result deleteXfRecord(@RequestBody EntityXfRecord entityXfRecord){
        try {
            Boolean saveXfRecord=xfRecordService.deleteXfRecord(entityXfRecord);
            if (saveXfRecord==null){
                return ResultUtil.error(ResultEnum.Err_2016);
            }
            if (saveXfRecord){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        }catch (Exception e){
            log.info("请求方式:【{}】,模块入库删除的数据:【{}】,异常信息:【{}】","消费订单的成功数据入库的模块入库失败",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_802);
        }

    }

    @PostMapping(UnifiendPayUrl.URL_updateXfRecord)
    public Result updateXfRecord(@RequestBody EntityXfRecord entityXfRecord){
        try {
            Boolean saveXfRecord=xfRecordService.updateXfRecord(entityXfRecord);
            if (saveXfRecord==null){
                return ResultUtil.error(ResultEnum.Err_2016);
            }
            if (saveXfRecord){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        }catch (Exception e){
            log.info("请求方式:【{}】,模块入库修改的数据:【{}】,异常信息:【{}】","消费订单的成功数据入库的模块入库失败",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_802);
        }

    }

    @PostMapping(UnifiendPayUrl.URL_updateXfFace)
    public Result updateXFace(@RequestBody EntityXfRecord entityXfRecord){
        try {
            Boolean saveXfRecord=xfRecordService.updateFace(entityXfRecord);
            if (saveXfRecord==null){
                return ResultUtil.error(ResultEnum.Err_3014);
            }
            if (saveXfRecord){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.Err_3014);
            }
        }catch (Exception e){
            log.info("请求方式:【{}】,模块入库修改的数据:【{}】,异常信息:【{}】","消费订单的成功数据入库的模块入库失败",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_3014);
        }

    }

    @PostMapping(UnifiendPayUrl.URL_getConsumeDetail)
    public Result getConsumeDetail(@RequestBody JSONObject jsonObject){
        try {
            if (jsonObject.getLong("comId")==null){
                return ResultUtil.error(ResultEnum.Err_203);
            }
            Page<EntityXfRecord> list=xfRecordService.webSelectDetails(jsonObject);
            Map<String,Object> object=new HashMap<>();
            object.put("total",list.getTotal());
            object.put("list",list.getRecords());
            return ResultUtil.success(object);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取消费记录信息",jsonObject,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    @PostMapping(UnifiendPayUrl.URL_getConsumeReports)
    public Result getConsumeReports(@RequestBody JSONObject jsonObject){
        try {
            if (jsonObject.getLong("comId")==null){
                return ResultUtil.error(ResultEnum.Err_203);
            }
            List<EntityXfMealInfo> listMealName=new ArrayList<>();

            try {
                listMealName=jsonObject.getObject("mealNameInfos",List.class);
            }catch (Exception e){

            }
            List<EntityXfRecord> list= new ArrayList<>();

            Integer orderType = jsonObject.getInteger("orderType");
            if (orderType == null || orderType == 1) {
                list = xfRecordService.getConsumeReports(jsonObject);
            } else {
                list = xfRecordService.getConsumeReports10(jsonObject);
            }

            List<EntityXfRecord> result2 = list.stream().sorted(Comparator.comparing(EntityXfRecord::getSTime).reversed()).collect(Collectors.toList());
            Map<String,Object> object=new HashMap<>();
            object.put("total",result2.size());
            List<EntityXfRecord> entityXfRecordList=new ArrayList<>();
            int page=jsonObject.getInteger("page");
            int rows=jsonObject.getInteger("size");
            if (page==-1 || rows==-1){
                entityXfRecordList=result2;
            }else {
                int currIdx = (page > 1 ? (page -1) * rows: 0);
                for (int i = 0; i < rows && i < result2.size() - currIdx; i++) { // 判断条件十分巧妙，防止List取值越界
                    EntityXfRecord active = result2.get(currIdx + i);
                    entityXfRecordList.add(active);
                }
            }

            List<Map<String,Object>> reLsit=new ArrayList<>();
            for (EntityXfRecord entityXfRecord : entityXfRecordList) {

                Map<String,Object> map=new HashMap<>();
                if (entityXfRecord.getAmount1() !=null){
                    map.put("amount1",entityXfRecord.getAmount1());
                    map.put("times1",entityXfRecord.getTimes1());
                }
                if (entityXfRecord.getAmount2() !=null){
                    map.put("amount2",entityXfRecord.getAmount2());
                    map.put("times2",entityXfRecord.getTimes2());
                }
                if (entityXfRecord.getAmount3() !=null){
                    map.put("amount3",entityXfRecord.getAmount3());
                    map.put("times3",entityXfRecord.getTimes3());
                }
                if (entityXfRecord.getAmount4() !=null){
                    map.put("amount4",entityXfRecord.getAmount4());
                    map.put("times4",entityXfRecord.getTimes4());
                }
                if (entityXfRecord.getAmount5() !=null){
                    map.put("amount5",entityXfRecord.getAmount5());
                    map.put("times5",entityXfRecord.getTimes5());
                }
                if (entityXfRecord.getAmount6() !=null){
                    map.put("amount6",entityXfRecord.getAmount6());
                    map.put("times6",entityXfRecord.getTimes6());
                }
                if (entityXfRecord.getAmount7() !=null){
                    map.put("amount7",entityXfRecord.getAmount7());
                    map.put("times7",entityXfRecord.getTimes7());
                }
                if (entityXfRecord.getAmount8() !=null){
                    map.put("amount8",entityXfRecord.getAmount8());
                    map.put("times8",entityXfRecord.getTimes8());
                }
                if (entityXfRecord.getAmount9() !=null){
                    map.put("amount9",entityXfRecord.getAmount9());
                    map.put("times9",entityXfRecord.getTimes9());
                }
                if (entityXfRecord.getAmount10() !=null){
                    map.put("amount10",entityXfRecord.getAmount10());
                    map.put("times10",entityXfRecord.getTimes10());
                }
                if (entityXfRecord.getAmount11() !=null){
                    map.put("amount11",entityXfRecord.getAmount11());
                    map.put("times11",entityXfRecord.getTimes11());
                }
                if (entityXfRecord.getAmount12() !=null){
                    map.put("amount12",entityXfRecord.getAmount12());
                    map.put("times12",entityXfRecord.getTimes12());
                }
                map.put("amount2",entityXfRecord.getAmount2());
                map.put("times2",entityXfRecord.getTimes2());
                map.put("amount3",entityXfRecord.getAmount3());
                map.put("times3",entityXfRecord.getTimes3());
                map.put("amountother",entityXfRecord.getAmountother());
                map.put("timesother",entityXfRecord.getTimesother());
                map.put("allAmount",entityXfRecord.getAllAmount());
                map.put("allTimes",entityXfRecord.getAllTimes());
                map.put("allRefund",entityXfRecord.getAllRefund());
                map.put("allRefundTimes",entityXfRecord.getAllRefundTimes());
                map.put("sTime",entityXfRecord.getSTime());
                map.put("merchName",entityXfRecord.getMerchName());
                map.put("pointName",entityXfRecord.getPointName());
                map.put("deviceNo",entityXfRecord.getDeviceNo());
                map.put("deviceName",entityXfRecord.getDeviceName());
                reLsit.add(map);
            }
            List<Map<String,Object>> xfMealInfoLsit=new ArrayList<>();
            for (int i = 0; i < listMealName.size(); i++) {
                EntityXfMealInfo entityXfMealInfo= JSON.parseObject(JSON.toJSONString(listMealName.get(i)),EntityXfMealInfo.class) ;
                Map<String,Object> map=new HashMap<>();
                map.put("mealNo",entityXfMealInfo.getMealNo());
                map.put("mealName",entityXfMealInfo.getMealName());
                xfMealInfoLsit.add(map);
            }
            object.put("list",reLsit);
            object.put("mealName",xfMealInfoLsit);
            return ResultUtil.success(object);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取消费记录信息",jsonObject,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    @PostMapping(UnifiendPayUrl.URL_getConsumeReportsC)
    public Result getConsumeReportsC(@RequestBody JSONObject jsonObject){
        try {
            if (jsonObject.getLong("comId")==null){
                return ResultUtil.error(ResultEnum.Err_203);
            }
            List<EntityXfMealInfo> listMealName=new ArrayList<>();

            try {
                listMealName=jsonObject.getObject("mealNameInfos",List.class);
            }catch (Exception e){

            }
            List<EntityXfRecord> list= new ArrayList<>();

            Integer orderType = jsonObject.getInteger("orderType");
            if (orderType == null || orderType == 1) {
                list = xfRecordService.getConsumeReportsC(jsonObject);
            } else {
                list = xfRecordService.getConsumeReports10C(jsonObject);
            }

            List<EntityXfRecord> result2 = list.stream().sorted(Comparator.comparing(EntityXfRecord::getSTime).reversed()).collect(Collectors.toList());
            Map<String,Object> object=new HashMap<>();
            object.put("total",result2.size());
            List<EntityXfRecord> entityXfRecordList=new ArrayList<>();
            int page=jsonObject.getInteger("page");
            int rows=jsonObject.getInteger("size");
            if (page==-1 || rows==-1){
                entityXfRecordList=result2;
            }else {
                int currIdx = (page > 1 ? (page -1) * rows: 0);
                for (int i = 0; i < rows && i < result2.size() - currIdx; i++) { // 判断条件十分巧妙，防止List取值越界
                    EntityXfRecord active = result2.get(currIdx + i);
                    entityXfRecordList.add(active);
                }
            }

            List<Map<String,Object>> reLsit=new ArrayList<>();
            for (EntityXfRecord entityXfRecord : entityXfRecordList) {

                Map<String,Object> map=new HashMap<>();
                if (entityXfRecord.getAmount1() !=null){
                    map.put("amount1",entityXfRecord.getAmount1());
                    map.put("times1",entityXfRecord.getTimes1());
                }
                if (entityXfRecord.getAmount2() !=null){
                    map.put("amount2",entityXfRecord.getAmount2());
                    map.put("times2",entityXfRecord.getTimes2());
                }
                if (entityXfRecord.getAmount3() !=null){
                    map.put("amount3",entityXfRecord.getAmount3());
                    map.put("times3",entityXfRecord.getTimes3());
                }
                if (entityXfRecord.getAmount4() !=null){
                    map.put("amount4",entityXfRecord.getAmount4());
                    map.put("times4",entityXfRecord.getTimes4());
                }
                if (entityXfRecord.getAmount5() !=null){
                    map.put("amount5",entityXfRecord.getAmount5());
                    map.put("times5",entityXfRecord.getTimes5());
                }
                if (entityXfRecord.getAmount6() !=null){
                    map.put("amount6",entityXfRecord.getAmount6());
                    map.put("times6",entityXfRecord.getTimes6());
                }
                if (entityXfRecord.getAmount7() !=null){
                    map.put("amount7",entityXfRecord.getAmount7());
                    map.put("times7",entityXfRecord.getTimes7());
                }
                if (entityXfRecord.getAmount8() !=null){
                    map.put("amount8",entityXfRecord.getAmount8());
                    map.put("times8",entityXfRecord.getTimes8());
                }
                if (entityXfRecord.getAmount9() !=null){
                    map.put("amount9",entityXfRecord.getAmount9());
                    map.put("times9",entityXfRecord.getTimes9());
                }
                if (entityXfRecord.getAmount10() !=null){
                    map.put("amount10",entityXfRecord.getAmount10());
                    map.put("times10",entityXfRecord.getTimes10());
                }
                if (entityXfRecord.getAmount11() !=null){
                    map.put("amount11",entityXfRecord.getAmount11());
                    map.put("times11",entityXfRecord.getTimes11());
                }
                if (entityXfRecord.getAmount12() !=null){
                    map.put("amount12",entityXfRecord.getAmount12());
                    map.put("times12",entityXfRecord.getTimes12());
                }
                map.put("amount2",entityXfRecord.getAmount2());
                map.put("times2",entityXfRecord.getTimes2());
                map.put("amount3",entityXfRecord.getAmount3());
                map.put("times3",entityXfRecord.getTimes3());
                map.put("amountother",entityXfRecord.getAmountother());
                map.put("timesother",entityXfRecord.getTimesother());
                map.put("allAmount",entityXfRecord.getAllAmount());
                map.put("allTimes",entityXfRecord.getAllTimes());
                map.put("allRefund",entityXfRecord.getAllRefund());
                map.put("allRefundTimes",entityXfRecord.getAllRefundTimes());
                map.put("allDiscount",entityXfRecord.getAllDiscount());
                map.put("sTime",entityXfRecord.getSTime());
                map.put("merchName",entityXfRecord.getMerchName());
                map.put("pointName",entityXfRecord.getPointName());
                map.put("deviceNo",entityXfRecord.getDeviceNo());
                map.put("deviceName",entityXfRecord.getDeviceName());
                reLsit.add(map);
            }
            List<Map<String,Object>> xfMealInfoLsit=new ArrayList<>();
            for (int i = 0; i < listMealName.size(); i++) {
                EntityXfMealInfo entityXfMealInfo= JSON.parseObject(JSON.toJSONString(listMealName.get(i)),EntityXfMealInfo.class) ;
                Map<String,Object> map=new HashMap<>();
                map.put("mealNo",entityXfMealInfo.getMealNo());
                map.put("mealName",entityXfMealInfo.getMealName());
                xfMealInfoLsit.add(map);
            }
            object.put("list",reLsit);
            object.put("mealName",xfMealInfoLsit);
            return ResultUtil.success(object);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取消费记录信息",jsonObject,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    //获取饭堂消费的情况
    @PostMapping(UnifiendPayUrl.URL_xfUserparam)
    public Result xfUserparam(@RequestBody EntityXfRecord entityXfRecord){
        try {
            EntityXfRecord xfRecord=xfRecordService.getxfUserparam(entityXfRecord);
            Map<String,Object> map=new HashMap<>();
            map.put("times",xfRecord.getTimes());
            if(xfRecord.getAmount()==null){
                xfRecord.setAmount(0l);
            }
            map.put("amount",xfRecord.getAmount());
            return ResultUtil.success(map);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取饭堂消费的情况",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    //获取饭堂消费优惠的情况
    @PostMapping(UnifiendPayUrl.URL_xfUserparamDiscount)
    public Result xfUserparamDiscount(@RequestBody EntityXfRecord entityXfRecord){
        try {
            EntityXfRecord xfRecord=xfRecordService.getxfUserparamDiscount(entityXfRecord);
            Map<String,Object> map=new HashMap<>();
            map.put("times",xfRecord.getTimes());
            if(xfRecord.getAmount()==null){
                xfRecord.setAmount(0l);
            }
            map.put("amount",xfRecord.getAmount());
            return ResultUtil.success(map);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取饭堂消费优惠的情况",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    //获取饭堂消费次数
    @PostMapping(UnifiendPayUrl.URL_times)
    public Result times(@RequestBody EntityXfRecord entityXfRecord){
        try {
            EntityXfRecord xfRecord=xfRecordService.getTimes(entityXfRecord);
            Map<String,Object> map=new HashMap<>();
            map.put("times",xfRecord.getTimes());
            return ResultUtil.success(map);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取饭堂消费次数",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    @PostMapping(UnifiendPayUrl.URL_getPointIdByNameFromRecord)
    public Result getPointIdByNameFromRecord(@RequestBody JSONObject jsonObject){
        try {
            EntityXfRecord xfRecord=xfRecordService.getPointIdByNameFromRecord(jsonObject);
            Map<String,Object> map=new HashMap<>();
            if (xfRecord==null){
                map.put("pointId",-1);
            }else {
                map.put("pointId",xfRecord.getPointId());
            }
            return ResultUtil.success(map);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","根据记录的名称获取消费点编号",jsonObject,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    @PostMapping(UnifiendPayUrl.URL_getEntityXfRecord)
    public Result getEntityXfRecord(@RequestParam String orderNo, @RequestParam String monthValue){
        try {
            EntityXfRecord xfRecord = xfRecordService.getEntityXfRecord(orderNo, monthValue);
            return ResultUtil.success(xfRecord);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }


    @PostMapping(UnifiendPayUrl.URL_orderVerification)
    public Result orderVerification(@RequestBody XfOrderVerificationRequest request){
        xfRecordService.updateOrderVerification(request);
        return ResultUtil.success();
    }

    @PostMapping(UnifiendPayUrl.URL_getPayDate)
    public Result getPayDate(@RequestParam Long comId,@RequestParam String payDate){
       return xfRecordService.getXfPayDate(comId,payDate);
    }

    //获取饭堂消费次数
    @PostMapping(UnifiendPayUrl.URL_timesManJian)
    public Result timesManJian(@RequestBody EntityXfRecord entityXfRecord){
        try {
            List<EntityXfRecord>  xfRecord=xfRecordService.getTimesManJian(entityXfRecord);
            Map<Long, Integer> merchPayCountMap = new HashMap<>();
            if(xfRecord==null || xfRecord.size()==0){
                return ResultUtil.success(merchPayCountMap);
            }
            //list转map
            merchPayCountMap= xfRecord.stream().collect(Collectors.toMap(e -> e.getDiscount(),
                    e -> e.getTimes(),
                    (e1, e2) -> e1));
            return ResultUtil.success(merchPayCountMap);
        }catch (Exception e){
            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取饭堂消费次数",entityXfRecord,e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }

    //11.4.4 获取用户已点餐餐段
    @PostMapping(UnifiendPayUrl.URL_getUserOrderedB)
    public Result getUserOrderedB(@RequestBody GetUserOrderedB request){
        try {
            return  ResultUtil.success(this.xfRecordService.getUserOrderedB(request));
        }catch (Exception e){
            log.info("查询");
            return  ResultUtil.info(500,e.getMessage());
        }

    }
}
